import sys

def create_kml(fn):
	from xml.dom.minidom import parse
	import string
	sf = open(fn)
	path_name = fn.rstrip('.txt')
	coordinate = ''
	for line in sf:
		lst_line = line.split(',')
		
		long_f = lst_line[5].split('.')[0]
		long_s = lst_line[5].split('.')[1]
		long_1 = int(long_f)/100
		long_2 = float('0.' + long_f[len(long_f)-2:] + long_s) * 1.6666
		longitude = str(long_1 + long_2)
		coordinate = coordinate + longitude + ','

		lat_f = lst_line[3].split('.')[0]
		lat_s = lst_line[3].split('.')[1]
		lat_1 = int(lat_f)/100
		lat_2 = float('0.' + lat_f[len(lat_f)-2:] + lat_s) * 1.6666
		latitude = str(lat_1 + lat_2)
		coordinate = coordinate + latitude + ' '

	dom = parse('path_tempelet.xml')
	eles = dom.getElementsByTagName("name")
	for ele in eles:
		name = dom.createTextNode(path_name)
		ele.appendChild(name)
	coor_eles = dom.getElementsByTagName("coordinates")
	name = dom.createTextNode(coordinate)
	coor_eles[0].appendChild(name)

	kml_f = file(path_name + '.kml', 'w')
	print >> kml_f, dom.toxml()

	kml_f.close()
	dom.unlink()

if __name__ == '__main__':
	fn = sys.argv[1]
	create_kml(fn)
